package com.mars.foodserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mars.foodserver.entity.Food;
import com.mars.foodserver.entity.FoodType;
import com.mars.foodserver.mapper.FoodMapper;
import com.mars.foodserver.mapper.FoodTypeMapper;
import com.mars.foodserver.request.FoodRequest;
import com.mars.foodserver.request.FoodTypeRequest;
import com.mars.foodserver.response.FoodResponse;
import com.mars.foodserver.response.FoodTypeDetailResponse;
import com.mars.foodserver.response.FoodTypeListResp;
import com.mars.foodserver.response.base.PageInfo;
import com.mars.foodserver.service.IFoodTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wq
 * @since 2021-07-19
 */
@Service
public class FoodTypeServiceImpl extends ServiceImpl<FoodTypeMapper, FoodType> implements IFoodTypeService {


    @Resource
    private FoodMapper foodMapper;

    @Override
    public PageInfo<FoodType> page(FoodTypeRequest request) {
        IPage<FoodType> page = new Page<>(request.getPageNumber(), request.getPageSize());
        LambdaQueryWrapper<FoodType> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(request.getName())) {
            wrapper.like(FoodType::getName, request.getName());
        }
        List<FoodType> records = baseMapper.selectPage(page, wrapper).getRecords();
        if (!CollectionUtils.isEmpty(records)) {
            return new PageInfo<>(page.getTotal(), records);
        }
        return null;
    }

    @Override
    public void add(FoodTypeRequest request) {
        FoodType foodType = new FoodType();
        BeanUtils.copyProperties(request, foodType);
        save(foodType);
    }

    @Override
    public void update(FoodTypeRequest request) {
        FoodType foodType = new FoodType();
        BeanUtils.copyProperties(request, foodType);
        updateById(foodType);
    }

    @Override
    public void delete(Integer id) {
        baseMapper.deleteById(id);
    }

    @Override
    public FoodType detail(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public List<FoodTypeListResp> foodTypeList() {

        LambdaQueryWrapper<FoodType> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(FoodType::getCreateTime);
        return baseMapper.selectList(wrapper).stream().map(x -> {
            FoodTypeListResp resp = new FoodTypeListResp();
            resp.setId(x.getId());
            resp.setName(x.getName());
            resp.setImgUrl(x.getImgUrl());
            return resp;
        }).collect(Collectors.toList());
    }

    @Override
    public FoodTypeDetailResponse detailFoodType(Integer id) {
        FoodTypeDetailResponse detailResponse = new FoodTypeDetailResponse();
        FoodType foodType = baseMapper.selectById(id);
        if (Objects.nonNull(foodType)) {
            detailResponse.setFoodTypeDescription(foodType.getDescription());
            Integer foodTypeId = foodType.getId();
            List<Food> foods = foodMapper.selectList(new LambdaQueryWrapper<Food>().
                    eq(Food::getFoodTypeId, foodTypeId));
            List<FoodResponse> list = foods.stream().map(x -> {
                FoodResponse response = new FoodResponse();
                BeanUtils.copyProperties(x, response);
                return response;
            }).collect(Collectors.toList());
            detailResponse.setFoodList(list);
            return detailResponse;
        }
        return null;
    }
}
